﻿using System;
using xycn.common;

namespace xycn.datacache
{
    /// <summary>
    /// 类工厂，数据库接口库，需要同时实现该接口
    /// </summary>
    public class AuthorizeFactory : IPluginFactory.IPluginFactory
    {
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public override IDataCache.IDataCache CreatePlugin()
        {
            return new MemDataCache();
        }
    }

    /// <summary>
    /// 缓存实现接口
    /// </summary>
    public class RedisDataCache : IDataCache.IDataCache
    {
        public void DelCache(string key)
        {
            try
            {
                xycn.common.RedisHelper.DelRedis(key);
            }
            catch (Exception ex)
            {
            }
        }

        public T GetCache<T>(string key)
        {
            try
            {
                return xycn.common.RedisHelper.GetRedis<T>(key);
            }
            catch (Exception ex)
            {
                return default(T);
            }
        }

        public dynamic GetCache(string key)
        {
            try
            {
                return xycn.common.RedisHelper.GetRedis(key);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool SetCache(string key, object value)
        {
            try
            {
                return xycn.common.RedisHelper.SetRedisObj(key, value);
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool SetCache(string key, object value, int minutes)
        {
            try
            {
                return xycn.common.RedisHelper.SetRedisObj(key, value, minutes);
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
